﻿using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Simple.Services.System.Article;
using Simple.Services.System.Article.Model;
using Simple.Services.System.ClassRoom;

namespace Simple.WebApi.Controllers.System;

/// <summary>
/// 文章管理
/// </summary>
[Route("api/Article/[action]")]
[ApiController]
[Authorize]
public class ArticleController : ControllerBase
{
    private readonly ArticleService _articleService;

    public ArticleController(ArticleService articleService)
    {
        _articleService = articleService;
    }

    /// <summary>
    /// 查询文章内容详情
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> GetInfo([FromQuery] Guid? Id)
    {
        var data = await _articleService.GetArticleAsync(Id);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 文章列表分页查询
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> Page([FromQuery] ArticlePageQueryModel model)
    {
        var data = await _articleService.GetArticlePageAsync(model);
        return AppResult.Status200OK(data: data);
    }

    /// <summary>
    /// 添加文章
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Add([FromForm] ArticleCreateOrUpdateModel model)
    {
        await _articleService.AddArticleAsync(model);
        return AppResult.Status200OK("新增成功");
    }

    /// <summary>
    /// 编辑文章
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Edit([FromForm] ArticleCreateOrUpdateModel model)
    {
        await _articleService.UpdateArticleAsync(model);
        return AppResult.Status200OK("更新成功");
    }

    /// <summary>
    /// 删除文章
    /// </summary>
    /// <param name="IdList"></param>
    /// <returns></returns>
    [HttpPost]
    public async Task<AppResult> Delete([FromBody] List<Guid?> IdList)
    {
        await _articleService.DeleteArticleAsync(IdList);
        return AppResult.Status200OK("删除成功");
    }

    /// <summary>
    /// 阅读文章
    /// </summary>
    /// <param name="Id">文章ID</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<AppResult> Read([FromQuery] Guid? Id)
    {
        var data = await _articleService.Read(Id);
        return AppResult.Status200OK(data: data);
    }
}
